function f = markup(theta2,v,x2,p,delta,ind_market,A1,sj,QI)
%global rna
ns=length(v);
numobs=length(delta);
expdelta=exp(delta);
nmrkt=max(ind_market);
%recover price

alpha=theta2(1,1);
sigmap=theta2(1,end);
ddelt=-alpha+sigmap*repmat(v(1,:),numobs,1); 
%ddelt=-(alpha+sigmap*repmat(vp,numobs,1))./p;  
expmu1=expmu(theta2,v,x2,p);  

s1=ind_shares(expdelta,expmu1,QI,ind_market); 

A2=zeros(numobs,numobs); 
f=zeros(numobs,1);
 
for q=1:nmrkt
    temp=find(ind_market==q);
    s11=s1(temp,:);
    s11star=s11.*ddelt(temp,:);
    A2(temp,temp)=diag(-mean(s11star.*(1-s11),2))+(1-eye(size(s11,1),size(s11,1))).*(s11star*s11'./ns); 
    A(temp,temp)=A1(temp,temp).*A2(temp,temp);
    f(temp,:)=A(temp,temp)\sj(temp,:);
end

%rna=[rna,rank(A)];